﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game1942.Library.View
{
    public class BackgroundView
    {
        // The image representing the background layer
        Texture2D backgroundLayer;

        // An array of positions of the background layer
        Vector2[] positions;

        // The speed which the background is moving
        int speed;

        //Construcotr
        //The bagroundLayer parameter is a Backgraund photograph
        //The screenHeight parameter will get whit GraphicsDevice.Viewport.Height command
        //The speed parameter will be greatter than zero
        public BackgroundView(int textureHeight, int screenHeight, int speed)
        {
            this.speed = speed;
            positions = new Vector2[screenHeight / textureHeight + 1];

            for (int i = 0; i < positions.Length; i++)
            {
                positions[i] = new Vector2(1, i * textureHeight - 5);
            }
        }

        //Load a texture for a background
        public void TextureLoad(Texture2D aTexture)
        {
            backgroundLayer = aTexture;
        }

        //Update the position of the background layer
        public void UpdatePosition()
        {
            // Update the positions of the background
            for (int i = 0; i < positions.Length; i++)
            {
                // Update the position of the screen by adding the speed
                positions[i].Y += speed;

                // Check if the texture is out of view then position it to the start of the screen
                if (positions[i].Y >= backgroundLayer.Height * (positions.Length - 1))
                {
                    positions[i].Y = -backgroundLayer.Height;
                }

            }
        }

        //Draw the background layer
        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < positions.Length; i++)
            {
                spriteBatch.Draw(backgroundLayer, positions[i], Color.White);
            }
        }
    }
}
